package cn.buk.tms.dto;

import cn.buk.tms.common.dto.CustomerDto;
import cn.buk.tms.entity.misc.*;
import cn.buk.tms.factory.CustomerDtoFactory;
import cn.buk.tms.flight.dto.KeyCustomerPolicyDto;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static cn.buk.tms.common.constant.TmsConstant.ID_TYPE_NI;
import static cn.buk.tms.common.constant.TmsConstant.ID_TYPE_PASSPORT;

/**
 * @author yfdai
 */
public class PassengerDto {

    private int id;

    private int enterpriseId;

    /**
     * 所属企业客户
     */
    private CustomerDto customer;

    /**
     * 在所属企业客户的部门
     */
    private CustomerDepartmentDto department;

    /**
     * 在所属企业客户的职位
     */
    private CustomerPositionDto position;

    private String name;

    private String nameCn;

    private String nameEn;

    private String firstName;

    private String lastName;

    private Integer gender;

    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date birthday;

    private List<PassengerIdInfoDto> idInfos;

    private List<PassengerFfpNoDto> ffpNos;

    /**
     * 用于前端传递证件号
     */
    @JsonIgnore
    private String idNo;

    @JsonIgnore
    private int idType;

    @JsonIgnore
    private int psgType;

    @JsonIgnore
    private String passportNo;

    @JsonIgnore
    private Date passportExpiry;

    private String nationality;

    private String email;

    private String mobile;

    private String phone;

    private String fax;

    /**
     * 机上座位的喜好（前排，后排，中部？靠窗？靠走道？）
     */
    private String hobby;

    /**
     * 常旅客卡号
     */
    @Deprecated
    private String ffpNo;

    /**
     * 可以乘坐的飞机等级
     */
    @Deprecated
    private String flightLevel;

    /**
     * 可以乘坐的火车等级
     */
    @Deprecated
    private String trainLevel;

    /**
     * VIP级别
     */
    private int vipLevel;

    private String remark;

    /**
     * 最近购买次数
     */
    private Integer latestBuyCount;

    /**
     * 最近一次消费
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm", timezone = "GMT+8")
    private Date recency;

    /**
     * 适用的机票政策
     */
    private List<KeyCustomerPolicyDto> flightPolicies;


    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date lastUpdate;

    public static PassengerDto createBy(Passenger passenger) {
        PassengerDto dto = new PassengerDto();

        BeanUtils.copyProperties(passenger, dto, "idInfos", "ffpNos", "customer", "department", "position");

        //证件信息
        dto.setIdInfos(new ArrayList<>());
        for(PassengerIdInfo idInfo: passenger.getIdInfos()) {
            PassengerIdInfoDto idInfoDto = new PassengerIdInfoDto();
            BeanUtils.copyProperties(idInfo, idInfoDto);

            dto.getIdInfos().add(idInfoDto);

            //以下代码用于导出乘机人信息时
            if (idInfo.getIdType() == ID_TYPE_NI) {
                dto.setIdNo(idInfo.getIdNo());
            } else if (idInfo.getId() == ID_TYPE_PASSPORT) {
                dto.setPassportNo(idInfo.getIdNo());
            }
        }

        if (dto.getIdInfos().isEmpty()) {
            //兼容之前的保存方式

            //身份证
            if (passenger.getIdNo() != null && passenger.getIdNo().trim().length() > 0) {
                PassengerIdInfoDto idInfoDto = new PassengerIdInfoDto();
                idInfoDto.setIdType(ID_TYPE_NI);

                idInfoDto.setIdNo(passenger.getIdNo().trim());

                dto.getIdInfos().add(idInfoDto);
            }
            //护照
            if (passenger.getPassportNo() != null && passenger.getPassportNo().trim().length() > 0) {
                PassengerIdInfoDto idInfoDto = new PassengerIdInfoDto();
                idInfoDto.setIdType(ID_TYPE_PASSPORT);
                idInfoDto.setIdNo(passenger.getPassportNo().trim());
                idInfoDto.setIdIssueNation(passenger.getNationality());
                idInfoDto.setIdValidDate(passenger.getPassportExpiry());

                dto.getIdInfos().add(idInfoDto);
            }
        }

        //常旅客卡号信息
        dto.setFfpNos(new ArrayList<>());
        for(PassengerFfpNo info: passenger.getFfpNos()) {
            PassengerFfpNoDto ffpDto = new PassengerFfpNoDto();
            BeanUtils.copyProperties(info, ffpDto);

            dto.getFfpNos().add(ffpDto);
        }


        //所属客户信息
        dto.setCustomer(CustomerDtoFactory.createByCustomer(passenger.getCustomer()));
        //在所属客户的部门
        dto.setDepartment(CustomerDepartmentDto.createBy(passenger.getDepartment()));
        //在所属客户的职位
        dto.setPosition(CustomerPositionDto.createdBy(passenger.getPosition()));

        return dto;
    }

    public static Passenger convertTo(PassengerDto dto, final int enterpriseId) {
        Passenger passenger = new Passenger();

        BeanUtils.copyProperties(dto, passenger, "idInfos", "ffpNos", "customer", "department", "position");

        passenger.setEnterpriseId(enterpriseId);


        //证件信息
        for(PassengerIdInfoDto idInfoDto: dto.getIdInfos()) {
            PassengerIdInfo idInfo = new PassengerIdInfo();
            BeanUtils.copyProperties(idInfoDto, idInfo);

            idInfo.setPassenger(passenger);
            idInfo.setEnterpriseId(enterpriseId);

            passenger.getIdInfos().add(idInfo);
        }

        //常旅客卡号信息
        for(PassengerFfpNoDto ffpDto: dto.getFfpNos()) {
            PassengerFfpNo ffp = new PassengerFfpNo();
            BeanUtils.copyProperties(ffpDto, ffp);

            ffp.setPassenger(passenger);
            ffp.setEnterpriseId(enterpriseId);

            passenger.getFfpNos().add(ffp);
        }

        //所属客户信息
        if (dto.getCustomer() != null && dto.getCustomer().getId() > 0) {
            Customer c = new Customer();
            c.setId(dto.getCustomer().getId());

            passenger.setCustomer(c);
        }
        //在所属客户的部门
        if (dto.getDepartment() != null && dto.getDepartment().getId() > 0) {
            CustomerDepartment department = new CustomerDepartment();
            department.setId(dto.getDepartment().getId());
            passenger.setDepartment(department);
        }
        //在所属客户的职位
        if (dto.getPosition() != null && dto.getPosition().getId() > 0) {
            CustomerPosition position = new CustomerPosition();
            position.setId(dto.getPosition().getId());
            passenger.setPosition(position);
        }

        return passenger;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getEnterpriseId() {
        return enterpriseId;
    }

    public void setEnterpriseId(int enterpriseId) {
        this.enterpriseId = enterpriseId;
    }

    public CustomerDto getCustomer() {
        return customer;
    }

    public void setCustomer(CustomerDto customer) {
        this.customer = customer;
    }

    public CustomerDepartmentDto getDepartment() {
        return department;
    }

    public void setDepartment(CustomerDepartmentDto department) {
        this.department = department;
    }

    public String getNameCn() {
        return nameCn;
    }

    public void setNameCn(String nameCn) {
        this.nameCn = nameCn;
    }

    public String getNameEn() {
        return nameEn;
    }

    public void setNameEn(String nameEn) {
        this.nameEn = nameEn;
    }

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getIdNo() {
        return idNo;
    }

    public void setIdNo(String idNo) {
        this.idNo = idNo;
    }

    public String getPassportNo() {
        return passportNo;
    }

    public void setPassportNo(String passportNo) {
        this.passportNo = passportNo;
    }

    public Date getPassportExpiry() {
        return passportExpiry;
    }

    public void setPassportExpiry(Date passportExpiry) {
        this.passportExpiry = passportExpiry;
    }

    public String getNationality() {
        return nationality;
    }

    public void setNationality(String nationality) {
        this.nationality = nationality;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getFax() {
        return fax;
    }

    public void setFax(String fax) {
        this.fax = fax;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    public String getFfpNo() {
        return ffpNo;
    }

    public void setFfpNo(String ffpNo) {
        this.ffpNo = ffpNo;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getLastUpdate() {
        return lastUpdate;
    }

    public void setLastUpdate(Date lastUpdate) {
        this.lastUpdate = lastUpdate;
    }

    public String getFlightLevel() {
        return flightLevel;
    }

    public void setFlightLevel(String flightLevel) {
        this.flightLevel = flightLevel;
    }

    public String getTrainLevel() {
        return trainLevel;
    }

    public void setTrainLevel(String trainLevel) {
        this.trainLevel = trainLevel;
    }

    public List<PassengerIdInfoDto> getIdInfos() {
        if (idInfos == null) {
            idInfos = new ArrayList<>();
        }
        return idInfos;
    }

    public void setIdInfos(List<PassengerIdInfoDto> idInfos) {
        this.idInfos = idInfos;
    }

    public int getVipLevel() {
        return vipLevel;
    }

    public void setVipLevel(int vipLevel) {
        this.vipLevel = vipLevel;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public CustomerPositionDto getPosition() {
        return position;
    }

    public void setPosition(CustomerPositionDto position) {
        this.position = position;
    }

    public List<PassengerFfpNoDto> getFfpNos() {
        if (ffpNos == null) {
            ffpNos = new ArrayList<>();
        }
        return ffpNos;
    }

    public void setFfpNos(List<PassengerFfpNoDto> ffpNos) {
        this.ffpNos = ffpNos;
    }


    public String getFirstName() {
        return firstName == null ? null: firstName.trim().toUpperCase();
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName == null ? null: lastName.trim().toUpperCase();
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int  getLatestBuyCount() {
        return latestBuyCount == null ? 0 : latestBuyCount;
    }

    public void setLatestBuyCount(Integer latestBuyCount) {
        this.latestBuyCount = latestBuyCount;
    }

    public Date getRecency() {
        return recency;
    }

    public void setRecency(Date recency) {
        this.recency = recency;
    }

    public List<KeyCustomerPolicyDto> getFlightPolicies() {
        if (flightPolicies == null) {
            flightPolicies = new ArrayList<>();
        }
        return flightPolicies;
    }


    public int getIdType() {
        return idType;
    }

    public void setIdType(int idType) {
        this.idType = idType;
    }

    public int getPsgType() {
        return psgType;
    }

    public void setPsgType(int psgType) {
        this.psgType = psgType;
    }
}
